
ARCH=$(shell uname -p)

ifeq ($(TARGET),)
ifeq ($(ARCH),x86_64)
TARGET=linux-x86_64
else
TARGET=linux-i386
endif
endif

ifeq ($(VERSION),)
VERSION=0.$(shell date +%y%m%d%H%M%S)
endif
BASEDIR     = $(shell cd .. && pwd)
SRCDIR      = $(BASEDIR)/sources
INSTALLDIR  = $(BASEDIR)/delivery

AVAILABLE_TARGETS = linux-i386 linux-x86_64 i686-w64-mingw32

ifdef DEBUG
CONFIG = Debug
else
CONFIG = Release
endif

ifeq ($(findstring $(TARGET),$(AVAILABLE_TARGETS)),)
all: help
clean: help
else

all: install

install: showArch cmake $(TARGET)_$(CONFIG) install_$(TARGET)_$(CONFIG)
clean: showArch clean_$(TARGET)_$(CONFIG)
cmake: showArch cmake_$(TARGET)_$(CONFIG)
cmakeclean: showArch cmakeclean_$(TARGET)_$(CONFIG)

endif

showArch:
	@echo "-------------------------"
	@echo "Building for $(TARGET)..."
	@echo "-------------------------"

$(TARGET)_$(CONFIG):
	make -C build/$(TARGET)_$(CONFIG) all

install_$(TARGET)_$(CONFIG):
	make -C build/$(TARGET)_$(CONFIG) install

clean_$(TARGET)_$(CONFIG):
	make -C build/$(TARGET)_$(CONFIG) clean

cmake_$(TARGET)_$(CONFIG):
	mkdir -p build/$(TARGET)_$(CONFIG)
	cd build/$(TARGET)_$(CONFIG) && cmake -DCMAKE_TOOLCHAIN_FILE=../../toolchains/$(TARGET).toolchain -DCMAKE_BUILD_TYPE=$(CONFIG) -DPOPUP_VERSION=$(VERSION) $(SRCDIR)

cmakeclean_$(TARGET)_$(CONFIG):
	rm -rf build/$(TARGET)_$(CONFIG)

help:
	@echo
	@echo "Available targets are: $(AVAILABLE_TARGETS)"
	@echo
